package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Users;
import com.qf.mapper.UsersMapper;
import com.qf.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project work
 * @Package com.qf.service.impl
 * @Date 2021/10/4 14:11
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;


    @Override
    public Users findUser(String name, String password) {
        boolean flag = findName(name);
        if(!flag){
            throw new RuntimeException("用户不存在");
        }
        Users users = usersMapper.findUser(name,password);
        if(users == null){
            throw new RuntimeException("用户名或密码错误");
        }
        if(users.getStatus() == 0){
            throw new RuntimeException("用户已冻结");
        }
        return users;
    }

    @Override
    public boolean findName(String name) {
        int count = usersMapper.findName(name);
        if (count > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<Users> findByPage(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Users> usersList = usersMapper.findByPage('%' + name + '%');
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);
        return pageInfo;
    }

    @Override
    public void updatePwd(Users users) {
        usersMapper.updateByPrimaryKeySelective(users);
    }
}
